//
//  TrendingCollectionsVC.m
//  YiShangPin
//
//  Created by yishangpin2 on 15/3/26.
//  Copyright (c) 2015年 yishangpin2. All rights reserved.
//

#import "TrendingCollectionsVC.h"
#import "Waterfall.h"
#import "MJRefresh.h"

@interface TrendingCollectionsVC (){
    int y;
    int y1;
    NSMutableArray *picmutArray;
    NSMutableArray *oldprice;
    NSMutableArray *newprice;
    float AllY;
    UIView *WaterfallView;

}

@end

@implementation TrendingCollectionsVC

- (void)viewDidLoad {
    [super viewDidLoad];
    WaterfallView = [[UIView alloc]init];
    
    picmutArray = [NSMutableArray arrayWithObjects:@"18",@"12",@"13",@"14",@"15",@"16",@"17",@"18",@"19",@"10",@"11",@"6", nil];
    oldprice = [NSMutableArray arrayWithObjects:@"168",@"129",@"200",@"234",@"321",@"157",@"123",@"453",@"290",@"310", nil];
    newprice = [NSMutableArray arrayWithObjects:@"68",@"89",@"100",@"134",@"221",@"97",@"83",@"200",@"190",@"210", nil];
    
    [self Waterfall_Upload];
    
    //    集成刷新控件
    [self setupRefresh];
    [self setupUpload];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void)viewWillAppear:(BOOL)animated{
    
    self.navigationItem.title = @"Trending Collections";
    
    
}
#pragma mark - 瀑布流排布

//瀑布流排布商品信息
- (void)Waterfall_Upload{
    //    UIImage *image = [UIImage imageNamed:picmutArray[4]];
    
    
    for (int i = 0; i<10; i++) {
        
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"Waterfall" owner:self options:nil];
        Waterfall *waterView = nib[0];
        
        waterView.tag = 1000 + [picmutArray[i] intValue];
        //        int Y = (y<y1)?y:y1;
        //判断是左边还是右边
        int Y;
        BOOL BOOLY;
        if (y<y1) {
            BOOLY = YES;
            Y = y;
        }
        else
        { BOOLY = NO;
            Y = y1;
        }
        int X = (y<y1)?0:self.view.frame.size.width/2;
        
        
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"fushi%@.jpg",picmutArray[i]]];
        //        [waterView ViewframSetX:0 SetY:0 Pic:image];
        [waterView ViewframSetX:X SetY:Y Pic:image delegateW:self.view.frame.size.width];
        [waterView SetOldPrice:[oldprice[i] floatValue] SetNewPrice:[newprice[i] floatValue]];
        
        if(BOOLY)
            y += waterView.viewY;
        else
            y1 += waterView.viewY;
        //        waterView.backgroundColor = [UIColor redColor];
        //        waterView.alpha = 0.5;
        
        [WaterfallView addSubview:waterView];
        [self.scrollview addSubview:WaterfallView];
        
    }
    
    AllY =+ (y<y1)?y1:y;
    WaterfallView.frame = CGRectMake(0, 0, self.view.frame.size.width, AllY);
    self.scrollview.contentSize = CGSizeMake(self.view.frame.size.width, AllY );
    
    
}

#pragma mark - 上拉加载

- (void)setupUpload{
    
    // 上拉加载更多(进入刷新状态就会调用self的footerRereshing)
    [self.scrollview addFooterWithTarget:self action:@selector(footerRereshing)];
    
    //    // 设置文字(也可以不设置,默认的文字在MJRefreshConst中修改)
    //    self.scrollView.footerPullToRefreshText = @"再往上拉一点!";
    //    self.scrollView.footerReleaseToRefreshText = @"现在可以松开了!";
    //    self.scrollView.footerRefreshingText = @"正在努力加载呢，别急~";
}

#pragma mark - 下拉刷新

- (void)setupRefresh{
    
    // 上拉加载更多(进入刷新状态就会调用self的footerRereshing)
    
    [self.scrollview addHeaderWithTarget:self action:@selector(headerRereshing)];
    
}

- (void)headerRereshing{
    
    //延迟0.5秒执行
    [self performSelector:@selector(stopRefresh) withObject:nil afterDelay:0.5f];
    
}

- (void)stopRefresh{
    
    [self.scrollview headerEndRefreshing];
    
}

- (void)footerRereshing{
    [self.scrollview footerEndRefreshing];
    //延迟0.5秒执行
    //    [self performSelector:@selector(stopUpload) withObject:nil afterDelay:0.5f];
    NSMutableArray *array1 = [[NSMutableArray alloc] initWithObjects:@"13",@"14",@"15",@"16",@"17",@"18",@"19",@"12",@"21",@"2", nil];
    
    NSMutableArray *array2 = [[NSMutableArray alloc] initWithObjects:@"11",@"10",@"22",@"17",@"7",@"6",@"19",@"4",@"3",@"16", nil];
    if ([picmutArray[0]  isEqual: @"11"]) {
        picmutArray = array1;
    }
    else{
        picmutArray = array2;
    }
    
    [self Waterfall_Upload];
    
}

- (void)stopUpload{
    
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Prompt" message:@"Has been to the bottom." delegate:self cancelButtonTitle:@"Ensure" otherButtonTitles:nil, nil];
    [alert show];
}

@end
